package com.itlong.cloud.property.resource;

import com.itlong.cloud.POJO.DTO.property.PropertyEquipAuthGetDTO;
import com.itlong.cloud.POJO.DTO.property.UserDeviceAuthAddreddDTO;
import com.itlong.cloud.POJO.PO.TimeTaskAuthDeviceAddressInfoPO;
import com.itlong.cloud.POJO.VO.property.AppUserGetDeviceAuthVO;
import com.itlong.cloud.POJO.VO.property.EquipAuthUserGetVO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipSimpleVO;
import com.itlong.cloud.equip.property.IPropertyDeviceAuthServiceClient;
import com.itlong.cloud.property.service.IPropertyDeviceAuthService;
import com.itlong.cloud.property.service.IPropertyEquipService;
import com.itlong.cloud.utils.page.Page;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      设备权限接口服务暴露接口实现类
 * </desc>
 *
 * @createDate 2017/10/13
 */
@RestController
public class PropertyDeviceAuthServiceResource implements IPropertyDeviceAuthServiceClient {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyDeviceAuthServiceResource.class);

    @Autowired
    private IPropertyEquipService iPropertyEquipService;

    @Autowired
    private IPropertyDeviceAuthService iPropertyDeviceAuthService;

    /**
     * <desc>
     *      合成并保存受设备地址变更影响的住户设备权限
     * <desc/>
     * @param  userDeviceAuthAddreddDTOS  地址信息集
     * @param  authSource  权限来源
     * @return
     * @author zhangs
     * @createDate 2017/11/2
     */
    @PostMapping(path = "/saveUserAuthByAddr")
    @Override
    public void saveUserAuthByAddr(@RequestBody List<UserDeviceAuthAddreddDTO> userDeviceAuthAddreddDTOS, Integer authSource) throws Exception {
        iPropertyDeviceAuthService.saveUserAuthByAddr(userDeviceAuthAddreddDTOS, authSource);
    }

    /**
     * <desc>
     *      迁出住户时删除地址对应的其他类别的设备权限
     * <desc/>
     * @param   userDeviceAuthAddreddDTOS  地址信息集
     * @return
     * @author zhangs
     * @createDate 2017/11/2
     */
    @PostMapping(path = "/delUserOtherAuth")
    @Override
    public void delUserOtherAuth(@RequestBody List<UserDeviceAuthAddreddDTO> userDeviceAuthAddreddDTOS) throws Exception {
        iPropertyDeviceAuthService.delUserOtherAuth(userDeviceAuthAddreddDTOS);
    }

    /**
     * <desc>
     *      设备授权-更新用户设备权限
     * <desc/>
     *
     * @param loginUserId 用户登录Id
     * @param userId 用户Id
     * @param projectId 项目Id
     * @param appUserGetDeviceAuthVOS 用户设备权限信息
     * @return Integer 影响的数据数量
     * @author zhangs
     * @createDate 2017/10/10
     */
    @RequestMapping(path = "manage/updateSmartCardUserDeviceAuth")
    @Override
    public Integer updateSmartCardUserDeviceAuth(
            @RequestParam("loginUserId") String loginUserId,
            @RequestParam("userId") String userId,
            @RequestParam("projectId") String projectId,
            @RequestBody List<AppUserGetDeviceAuthVO> appUserGetDeviceAuthVOS) throws Exception{
        return iPropertyDeviceAuthService.updateSmartCardUserDeviceAuth(loginUserId, userId, projectId, appUserGetDeviceAuthVOS);
    }

    /**
     * <desc>
     *      家半径临时授权（包括住户管理设备授权和家半径一卡通用户授权）设备授权-更新用户临时设备权限(物业设备授权按钮)
     *      前端页面只能一次传一个页面的数据，传当前页面所有设备以及勾选上的设备(电梯设备还需传楼层信息)
     * <desc/>
     *
     * @param loginUserId 用户登录Id
     * @param userId 用户Id
     * @param projectId 项目Id
//     * @param authSource 来源标识
     * @param deviceType 设备类型
     * @param currentDevices 当前页面所有设备唯一码，以英文逗号拼接
     * @param selectDevices 当前页面所有选中设备唯一码，以英文逗号拼接
     * @param naturalfloorList 选中电梯设备时传输的楼层信息
     * @return
     * @author zhangs
     * @createDate 2017/10/10
     */
    @RequestMapping(path = "manage/updateHouseHoldTmpDeviceAuth", method = RequestMethod.POST)
    @Override
    public void updateHouseHoldTmpDeviceAuth(
            @RequestParam(value = "loginUserId",required = false) String loginUserId,
            @RequestParam("userId") String userId,
            @RequestParam("projectId") String projectId,
//            @RequestParam("authSource") Integer authSource,
            @RequestParam("deviceType") Integer deviceType,
            @RequestParam(value = "currentDevices",required = false) String currentDevices,
            @RequestParam(value = "selectDevices",required = false) String selectDevices,
            @RequestBody List<String> naturalfloorList,
            @RequestParam("mark") String mark) throws Exception {
        iPropertyDeviceAuthService.updateHouseHoldTmpDeviceAuth(loginUserId, userId, projectId, deviceType,
                currentDevices, selectDevices, naturalfloorList,mark);
    }

    /**
     * <desc>
     *      设备授权-获取用户设备权限
     * <desc/>
     *
     * @param userId 用户Id
     * @param projectId 项目Id
     * @return List<AppUserGetDeviceAuthVO> 用户设备权限信息
     * @author zhangs
     * @createDate 2017/10/10
     */
    @RequestMapping(path = "manage/getUserDeviceAuth")
    @Override
    public List<AppUserGetDeviceAuthVO> getUserDeviceAuth(@RequestParam("userId") String userId,
                                                          @RequestParam("projectId") String projectId,
                                                          @RequestParam("deviceType") String deviceType) throws Exception{
        return iPropertyDeviceAuthService.getUserDeviceAuth(userId, projectId, deviceType);
    }

    /**
     * <desc>
     *      查询待刷新权限的地址对象集
     * <desc/>
     * @param  params 查询参数
     * @return  查询到受影响地址集合
     * @author Biao.R
     * @createDate 2018/2/7
     */
    @Override
    @RequestMapping(path = "manage/getNotFreshAddrByParams", method = RequestMethod.POST)
    public List<TimeTaskAuthDeviceAddressInfoPO> getNotFreshAddrByParams(@RequestBody Map<String, Object> params) {
        return iPropertyDeviceAuthService.getNotFreshAddrByParams(params);
    }

    /**
     * <desc>
     *      获取缓存中标记设备地址变更刷新用户权限定时任务执行状态
     * <desc/>
     * @param
     * @return boolean
     * @author Biao.R
     * @createDate 2018/2/7
     */
    @Override
    @RequestMapping(path = "manage/getDeviceAuthTimeTaskExexuterStatus", method = RequestMethod.POST)
    public boolean getDeviceAuthTimeTaskExexuterStatus() {
        return iPropertyDeviceAuthService.getDeviceAuthTimeTaskExexuterStatus();
    }

    /**
     * <desc>
     *      在缓存中标记设备地址变更刷新用户权限定时任务执行状态
     * <desc/>
     * @param  isWorking  定时任务执行状态(true:正在执行|false:未执行)
     * @return
     * @author Biao.R
     * @createDate 2018/2/7
     */
    @Override
    @RequestMapping(path = "manage/updateDeviceAuthTimeTaskExexuterStatus", method = RequestMethod.POST)
    public void updateDeviceAuthTimeTaskExexuterStatus(@RequestParam("isWorking") boolean isWorking) {
        iPropertyDeviceAuthService.updateDeviceAuthTimeTaskExexuterStatus(isWorking);
    }

    /**
     * <desc>
     *      定时任务刷新用户权限
     * <desc/>
     * @param   userDeviceAuthAddreddDTOS  地址信息集
     * @param   authSource  权限来源
     * @param   handlerTime  执行时间
     * @return  定时任务表被执行的个数
     * @author Biao.R
     * @createDate 2018/2/8
     */
    @RequestMapping(path = "manage/updateAddrUserAuthByTimeTask", method = RequestMethod.POST)
    @Override
    public Integer updateAddrUserAuthByTimeTask(@RequestBody List<UserDeviceAuthAddreddDTO> userDeviceAuthAddreddDTOS,
                                                @RequestParam("authSource") Integer authSource,
                                                @RequestParam("handlerTime")String handlerTime) {
        return iPropertyDeviceAuthService.updateAddrUserAuthByTimeTask(userDeviceAuthAddreddDTOS,authSource,handlerTime);
    }

    /**
     * <desc>
     *      通知其它服务清缓存
     * <desc/>
     * @param   list
     * @return
     * @author Juguang.S
     * @createDate 2018/4/17
     */
    @RequestMapping(path = "manage/noticeOtherServiceRedisBySamrtCard", method = RequestMethod.POST)
    public void noticeOtherServiceRedisBySamrtCard(@RequestBody List<EquipAuthUserGetVO> list){
        iPropertyDeviceAuthService.noticeOtherServiceRedisBySamrtCard(list);
    };

    /**
     * <desc>
     *     执行调度smart_card_face_pull_cloud_info表中的大于允许同步时间的数据推送至人脸识别系统
     * </desc>
     *
     * @author Juguang.S
     * @createDate 2019/07/06
     */
    @RequestMapping(path = "/smartCardFace/timeTaskPushSamrtCardFace", method = RequestMethod.POST)
    public void timeTaskPushSamrtCardFace(){
        iPropertyDeviceAuthService.timeTaskPushSamrtCardFace();
    }

    /**
     * <desc>
     *      获取用户有权限的设备分页列表
     * </desc>
     *
     * @param  propertyEquipAuthGetDTO 查询参数DTO
     * @return 用户有权限的设备分页列表
     * @Author Zhu.yj
     * @createDate  2021/4/21
     */
    @RequestMapping("/setting/user/getUserHaveAuthDevice")
    @Override
    public Page<PropertyEquipSimpleVO> getUserHaveAuthDevice(@RequestBody PropertyEquipAuthGetDTO propertyEquipAuthGetDTO) throws Exception {
        return iPropertyDeviceAuthService.getUserHaveAuthDevice(propertyEquipAuthGetDTO);
    }

    /**
     * <desc>
     *      删除用户电梯和门禁的分组权限
     * </desc>
     *
     * @param projectId
     * @param userId
     * @return
     * @Author Zhu.yj
     * @createDate  2021/5/11
     */
    @RequestMapping("/setting/user/deleteUserDeviceAuth")
    @Override
    public Integer deleteUserDeviceAuth(@RequestParam("projectId") String projectId, @RequestParam("userId") String userId) throws Exception {
        return iPropertyDeviceAuthService.deleteUserDeviceAuth(projectId, userId);
    }

}
